<?php

class MAISponsors extends FTAObject {
		
		var $renderSponsorsFunction;
	
		function MAISponsors(){
			parent::__construct();
				
			global $db;	
			$this->cmsDocument = new CmsDocument($db);
			
			global $FTADateFormat;
			$this->dateFormat = (0 < strlen($FTADateFormat)) ? $FTADateFormat : "d F y";
		}
		
		function getSponsors($pageSize = null, $pageNumber = null, $sort = null, $direction = null){
			//this gets sponsors and filters based on the category if there is one set.. 
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docSponsor');
						
			if(!is_null($sort)){
				if(!is_null($direction)){
					$this->cmsDocument->addSort($sort, $direction);
				} else {
					$this->cmsDocument->addSort($sort);
				}
			} else {
				$this->cmsDocument->addSort("<<md.rank>>");
			}
			
			$this->cmsDocument->setPageSize($pageSize);
			$this->cmsDocument->setPageNumber($pageNumber);
			
			$this->cmsDocument->resetIterator();
			
			$allDocuments = $this->cmsDocument->getAllDocuments();
			
			if (!$allDocuments)
				$allDocuments = array();
				
			return $allDocuments;
						
		}
		
		
		function renderSponsors(){
			$sponsors = $this->getSponsors();
			
			if(is_null($this->renderSponsorsFunction)) {
				
				global $MAISponsorsItemRenderFunction;
				if(!isset($MAISponsorsItemRenderFunction)){
					$MAISponsorsItemRenderFunction = '$o=fta_getSharedSponsorsObject(); echo $o->renderSponsorsItemFunction($item);';
				}
				//echo "Into Here!";
				$this->renderSponsorsFunction  = create_function('$item', $MAISponsorsItemRenderFunction );
			}
				
			//we need to map the links and do some funky scope wrandling..
			mai_sponsors_widgetList($sponsors,"sponsor-list", null, null, null,$this->renderSponsorsFunction);
		}
		
		function renderSponsorsItemFunction(){
			
		}
		
}

	function mai_renderSponsors(){
		ob_start();
		$sponsorObject = mai_getSharedSponsorsObject();
		$sponsorObject->renderSponsors();
		return ob_get_clean();	
	}

	function mai_getSharedSponsorsObject(){

		global $fta_;

		global $MAISponsorsClass;
		
		if(!is_null($MAISponsorsClass) && "MAISponsors" != $MAISponsorsClass){
		} else {
			$MAISponsorsClass = "MAISponsors";
		}
			
		if (!array_key_exists('maiSponsors',$fta_)){
			$fta_->maiSponsors = new  $MAISponsorsClass();
		}
		return $fta_->maiSponsors;
	}

	function mai_sponsors_widgetList($items,$listClass="list",$altItemClass='altItem',$firstItemClass='firstItem', $lastItemClass='lastItem',$renderItemFunction) {
?>
<ul id="<?= $listClass ?>">
<?		
	$row = -1;
	$numRows = count($items);
	
	foreach($items as $item){
		
		$itemClasses=array();
		
		if ($altItemClass && 0 == ($row++) % 2 ) 
			array_push($itemClasses,$altItemClass);
		
		if ($firstItemClass && 0 == $row)
			array_push($itemClasses,$firstItemClass);			
			
		if ($lastItemClass && $numRows -1 == $row)
			array_push($itemClasses,$lastItemClass);

?>		
		<li <?= ( 0 < count($itemClasses) ? "class=\"" . implode(" ",$itemClasses) . "\"" : "") ?> ><?= $renderItemFunction( $item ) ?></li>		
<?		
	}
?>
</ul>
<?		
	}
?>